using Newtonsoft.Json;
using System.Collections.Generic;

namespace BanZuiCloudControlAgent.Core.Mqtt.Models
{
    /// <summary>
    /// 任务上报消息负载模型
    /// </summary>
    public class TaskReportPayloadModel
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        [JsonProperty("taskId")]
        public string TaskId { get; set; }

        /// <summary>
        /// 任务类型
        /// </summary>
        [JsonProperty("taskType")]
        public string TaskType { get; set; }

        /// <summary>
        /// 任务状态
        /// </summary>
        [JsonProperty("taskStatus")]
        public string TaskStatus { get; set; }

        /// <summary>
        /// 任务进度（0-100）
        /// </summary>
        [JsonProperty("progress")]
        public int Progress { get; set; }

        /// <summary>
        /// 任务结果描述
        /// </summary>
        [JsonProperty("result")]
        public string Result { get; set; }

        /// <summary>
        /// 任务数据
        /// </summary>
        [JsonProperty("data")]
        public Dictionary<string, object> Data { get; set; }
    }

    /// <summary>
    /// 任务上报消息模型
    /// </summary>
    public class TaskReportModel : BaseMessageModel
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public TaskReportModel() : base()
        {
            MessageType = MessageTypeConstants.TASK_REPORT;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="payload">任务上报负载</param>
        public TaskReportModel(string deviceCode, TaskReportPayloadModel payload)
            : base(MessageTypeConstants.TASK_REPORT, deviceCode, payload)
        {
        }

        /// <summary>
        /// 获取任务上报负载
        /// </summary>
        /// <returns>任务上报负载</returns>
        public TaskReportPayloadModel GetPayload()
        {
            return Payload as TaskReportPayloadModel;
        }

        /// <summary>
        /// 创建任务开始上报消息
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="taskId">任务ID</param>
        /// <param name="taskType">任务类型</param>
        /// <returns>任务上报消息模型</returns>
        public static TaskReportModel CreateTaskStartReport(string deviceCode, string taskId, string taskType)
        {
            var payload = new TaskReportPayloadModel
            {
                TaskId = taskId,
                TaskType = taskType,
                TaskStatus = TaskStatusConstants.RUNNING,
                Progress = 0,
                Result = "任务开始执行",
                Data = new Dictionary<string, object>()
            };

            return new TaskReportModel(deviceCode, payload);
        }


        /// <summary>
        /// 创建任务暂停上报消息
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="taskId">任务ID</param>
        /// <param name="taskType">任务类型</param>
        /// <returns>任务上报消息模型</returns>
        public static TaskReportModel CreateTaskPauseReport(string deviceCode, string taskId, string taskType)
        {
            var payload = new TaskReportPayloadModel
            {
                TaskId = taskId,
                TaskType = taskType,
                TaskStatus = TaskStatusConstants.PAUSED,
                Progress = 0,
                Result = "任务已停止",
                Data = new Dictionary<string, object>()
            };
            return new TaskReportModel(deviceCode, payload);
        }
        
        /// <summary>
        /// 创建任务进度上报消息
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="taskId">任务ID</param>
        /// <param name="taskType">任务类型</param>
        /// <param name="progress">进度</param>
        /// <param name="result">结果描述</param>
        /// <param name="data">数据</param>
        /// <returns>任务上报消息模型</returns>
        public static TaskReportModel CreateTaskProgressReport(
            string deviceCode,
            string taskId,
            string taskType,
            int progress,
            string result,
            Dictionary<string, object> data = null)
        {
            var payload = new TaskReportPayloadModel
            {
                TaskId = taskId,
                TaskType = taskType,
                TaskStatus = TaskStatusConstants.RUNNING,
                Progress = progress,
                Result = result,
                Data = data ?? new Dictionary<string, object>()
            };

            return new TaskReportModel(deviceCode, payload);
        }

        /// <summary>
        /// 创建任务完成上报消息
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="taskId">任务ID</param>
        /// <param name="taskType">任务类型</param>
        /// <param name="result">结果描述</param>
        /// <param name="data">数据</param>
        /// <returns>任务上报消息模型</returns>
        public static TaskReportModel CreateTaskSuccessReport(
            string deviceCode,
            string taskId,
            string taskType,
            string result,
            Dictionary<string, object> data = null)
        {
            var payload = new TaskReportPayloadModel
            {
                TaskId = taskId,
                TaskType = taskType,
                TaskStatus = TaskStatusConstants.SUCCESS,
                Progress = 100,
                Result = result,
                Data = data ?? new Dictionary<string, object>()
            };

            return new TaskReportModel(deviceCode, payload);
        }

        /// <summary>
        /// 创建任务失败上报消息
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="taskId">任务ID</param>
        /// <param name="taskType">任务类型</param>
        /// <param name="errorMessage">错误信息</param>
        /// <param name="data">数据</param>
        /// <returns>任务上报消息模型</returns>
        public static TaskReportModel CreateTaskFailedReport(
            string deviceCode,
            string taskId,
            string taskType,
            string errorMessage,
            Dictionary<string, object> data = null)
        {
            var payload = new TaskReportPayloadModel
            {
                TaskId = taskId,
                TaskType = taskType,
                TaskStatus = TaskStatusConstants.FAILED,
                Progress = 0,
                Result = errorMessage,
                Data = data ?? new Dictionary<string, object>()
            };

            return new TaskReportModel(deviceCode, payload);
        }
    }
}